#include <QtCore/QCoreApplication>
#include <QDir>
#include <QFile>
#include <QString>
#include <QList>
#include <QtDBus>

#include <fstream>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    int i =0;

    while (!QDBusConnection::sessionBus().isConnected()) {
        QString dbus="/usr/bin/dbus-daemon --system &";
        system(dbus.toStdString().c_str());
        i++;
        if (i>=20){
            qWarning("Cannot connect to the D-Bus session bus.\n"
               "Please check your system settings and try again.\n");
            return 1;
        }
    }

    QString home, setdir, cfg;
    QString bindir, pixmapsdir;
    QList <QString> apps;

    home = QDir::homePath();
    setdir = "/.opendesk/";
    cfg = "config/";

    QDir dirchk;

    if (dirchk.exists(home+setdir) && dirchk.exists(home+setdir+cfg)){
        QString dir= home+setdir+cfg+"dir";
        qWarning(dir.toStdString().c_str());
        string stream;
        ifstream fin(dir.toStdString().c_str());
        while (fin >> stream) {
            if (stream[0]=='#') {
                QString warning = "commento: "+ QString::fromStdString(stream);
                qWarning(warning.toStdString().c_str());
            }
            if (stream == "BIN"){
                fin >> stream;
                bindir= QString::fromStdString(stream);
            }
            if (stream == "PIXMAPS"){
                fin >> stream;
                pixmapsdir=QString::fromStdString(stream);
            }
        }
        fin.close();
        dir=home+setdir+cfg+"apps";
        fin.open(dir.toStdString().c_str());
        while (fin >> stream) {
            if (stream.at(0)=='#') {
                QString warning = "commento: "+ QString::fromStdString(stream);
                qWarning(warning.toStdString().c_str());
            }
            if (stream == "APP"){
                fin >> stream;
                apps.push_back(QString::fromStdString(stream));
            }
        }
        fin.close();
    }
    else{
        QString dir= cfg+"dir";
        string stream;
        ifstream fin(dir.toStdString().c_str());
        while (fin >> stream) {
            if (stream[0]=='#') {
                QString warning = "commento: "+ QString::fromStdString(stream);
                qWarning(warning.toStdString().c_str());
            }
            if (stream == "BIN"){
                fin >> stream;
                bindir= QString::fromStdString(stream);
            }
            if (stream == "PIXMAPS"){
                fin >> stream;
                pixmapsdir=QString::fromStdString(stream);
            }
        }
        fin.close();
        dir=cfg+"apps";
        fin.open(dir.toStdString().c_str());
        while (fin >> stream) {
            if (stream.at(0)=='#') {
                QString warning = "commento: "+ QString::fromStdString(stream);
                qWarning(warning.toStdString().c_str());
            }
            if (stream == "APP"){
                fin >> stream;
                apps.push_back(QString::fromStdString(stream));
            }
        }
        fin.close();
        dirchk.mkdir(home+setdir);
        dirchk.mkdir(home+setdir+cfg);
        QString dircp, appscp, lookcp;
        dircp="cp "+cfg+"dir "+ home+setdir+cfg;
        appscp="cp "+cfg+"apps "+ home+setdir+cfg;
        lookcp="cp "+cfg+"look "+ home+setdir+cfg;
        system(dircp.toStdString().c_str());
        system(appscp.toStdString().c_str());
        system(lookcp.toStdString().c_str());
    }
    QString app;
    for (int i=0; i<apps.size(); i++) {
        app=bindir+apps.at(i)+"&";
        system(app.toStdString().c_str());
    }
    return a.exec();
}
